package com.jeesite.modules.ic.service.wx;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 微信相关接口调用业务处理
 */
@Service
@Slf4j
public class WxApiService {

    @Value("${wx.app.appid}")
    private String appAppID;

    @Value("${wx.app.secret}")
    private String appAppSecret;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * APP获取微信的AccessToken
     * @param code
     * @return
     */
    public String getAppAccessToken(String code) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appAppID+"&secret="+appAppSecret+"&code="+code+"&grant_type=authorization_code";
        log.error(url);
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }

    /**
     * APP获取微信的用户信息
     * @param
     * @return
     */
    public String getAppUserInfo(String openId, String accessToken) {
        String url = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId;
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }
}
